package utils

import (
	"fmt"
	"math/rand"
	"path/filepath"
	"strings"
)

func GenerateFileName(originalName string) string {
	ext := filepath.Ext(originalName)
	fileName := strings.TrimSuffix(originalName, ext)
	return fileName + "_" + RandomString(8) + ext
}

// RandomString 生成给定长度的随机字符串
func RandomString(length int) string {
	const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	b := make([]byte, length)
	for i := range b {
		b[i] = charset[rand.Intn(len(charset))]
	}
	return string(b)
}
func GetContentTypeByExtension(fileName string) (string, error) {
	extension := filepath.Ext(fileName)
	extension = strings.ToLower(extension)
	contentTypes := map[string]string{
		".jpg":  "image/jpeg",
		".jpeg": "image/jpeg",
		".png":  "image/png",
		".gif":  "image/gif",
		".txt":  "text/plain",
		".pdf":  "application/pdf",
		// 添加更多文件类型的映射
	}

	contentType, found := contentTypes[extension]
	if !found {
		return "", fmt.Errorf("unsupported file extension: %s", extension)
	}

	return contentType, nil
}
